<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单id" prop="orderId">
      <el-input size="mini"  v-model="dataForm.orderId" placeholder="订单id"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodsId">
      <el-input size="mini"  v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品单价" prop="goodsPrice">
      <el-input size="mini"  v-model="dataForm.goodsPrice" placeholder="商品单价"></el-input>
    </el-form-item>
    <el-form-item label="购买单个商品数量" prop="goodsNumber">
      <el-input size="mini"  v-model="dataForm.goodsNumber" placeholder="购买单个商品数量"></el-input>
    </el-form-item>
    <el-form-item label="商品小计价格" prop="goodsTotalPrice">
      <el-input size="mini"  v-model="dataForm.goodsTotalPrice" placeholder="商品小计价格"></el-input>
    </el-form-item>
    <el-form-item label="门店id" prop="storeId">
      <el-input size="mini"  v-model="dataForm.storeId" placeholder="门店id"></el-input>
    </el-form-item>
    <el-form-item label="门店名称" prop="storeName">
      <el-input size="mini"  v-model="dataForm.storeName" placeholder="门店名称"></el-input>
    </el-form-item>
    <el-form-item label="门店编码" prop="storeCode">
      <el-input size="mini"  v-model="dataForm.storeCode" placeholder="门店编码"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input size="mini"  v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input size="mini"  v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createUsername">
      <el-input size="mini"  v-model="dataForm.createUsername" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建人id" prop="createUserId">
      <el-input size="mini"  v-model="dataForm.createUserId" placeholder="创建人id"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="updateUsername">
      <el-input size="mini"  v-model="dataForm.updateUsername" placeholder="修改人"></el-input>
    </el-form-item>
    <el-form-item label="修改人id" prop="updateUserId">
      <el-input size="mini"  v-model="dataForm.updateUserId" placeholder="修改人id"></el-input>
    </el-form-item>
    <el-form-item label="是否可用 1:是" prop="status">
      <el-input size="mini"  v-model="dataForm.status" placeholder="是否可用 1:是"></el-input>
    </el-form-item>
    <el-form-item label="是否删除 1:是" prop="isDeleted">
      <el-input size="mini"  v-model="dataForm.isDeleted" placeholder="是否删除 1:是"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input size="mini"  v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderId: '',
          goodsId: '',
          goodsPrice: '',
          goodsNumber: '',
          goodsTotalPrice: '',
          storeId: '',
          storeName: '',
          storeCode: '',
          createTime: '',
          updateTime: '',
          createUsername: '',
          createUserId: '',
          updateUsername: '',
          updateUserId: '',
          status: '',
          isDeleted: '',
          remark: ''
        },
        dataRule: {
          orderId: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '商品单价不能为空', trigger: 'blur' }
          ],
          goodsNumber: [
            { required: true, message: '购买单个商品数量不能为空', trigger: 'blur' }
          ],
          goodsTotalPrice: [
            { required: true, message: '商品小计价格不能为空', trigger: 'blur' }
          ],
          storeId: [
            { required: true, message: '门店id不能为空', trigger: 'blur' }
          ],
          storeName: [
            { required: true, message: '门店名称不能为空', trigger: 'blur' }
          ],
          storeCode: [
            { required: true, message: '门店编码不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          createUsername: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          updateUsername: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          updateUserId: [
            { required: true, message: '修改人id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '是否可用 1:是不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除 1:是不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sale/saleorderitem/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderId = data.saleOrderItem.orderId
                this.dataForm.goodsId = data.saleOrderItem.goodsId
                this.dataForm.goodsPrice = data.saleOrderItem.goodsPrice
                this.dataForm.goodsNumber = data.saleOrderItem.goodsNumber
                this.dataForm.goodsTotalPrice = data.saleOrderItem.goodsTotalPrice
                this.dataForm.storeId = data.saleOrderItem.storeId
                this.dataForm.storeName = data.saleOrderItem.storeName
                this.dataForm.storeCode = data.saleOrderItem.storeCode
                this.dataForm.createTime = data.saleOrderItem.createTime
                this.dataForm.updateTime = data.saleOrderItem.updateTime
                this.dataForm.createUsername = data.saleOrderItem.createUsername
                this.dataForm.createUserId = data.saleOrderItem.createUserId
                this.dataForm.updateUsername = data.saleOrderItem.updateUsername
                this.dataForm.updateUserId = data.saleOrderItem.updateUserId
                this.dataForm.status = data.saleOrderItem.status
                this.dataForm.isDeleted = data.saleOrderItem.isDeleted
                this.dataForm.remark = data.saleOrderItem.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sale/saleorderitem/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderId': this.dataForm.orderId,
                'goodsId': this.dataForm.goodsId,
                'goodsPrice': this.dataForm.goodsPrice,
                'goodsNumber': this.dataForm.goodsNumber,
                'goodsTotalPrice': this.dataForm.goodsTotalPrice,
                'storeId': this.dataForm.storeId,
                'storeName': this.dataForm.storeName,
                'storeCode': this.dataForm.storeCode,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'createUsername': this.dataForm.createUsername,
                'createUserId': this.dataForm.createUserId,
                'updateUsername': this.dataForm.updateUsername,
                'updateUserId': this.dataForm.updateUserId,
                'status': this.dataForm.status,
                'isDeleted': this.dataForm.isDeleted,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
